import time

from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy
from selenium.common import NoSuchElementException, InvalidElementStateException

desired_caps = {
    "platformName": "Android",
    "platformVersion": "9.0",
    "deviceName": "SM-N9700",
    "appPackage": "com.eg.android.AlipayGphone",
    "appActivity": "com.eg.android.AlipayGphone.AlipayLogin",
    "noReset": True,
    "newCommandTimeout": 6000,
    "automationName": "UiAutomator2"
}
# 连接Appium Server，初始化自动化环境
driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps)

# 设置缺省等待时间
driver.implicitly_wait(15)

# 账号在其他设备登录
try:
    ensure = driver.find_element(AppiumBy.ID, 'com.alipay.mobile.antui:id/ensure')
    ensure.click()
except NoSuchElementException as e:
    pass

try:
    first_ele = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                    'new UiSelector().resourceId("com.alipay.android.phone.openplatform:id/app_text").text("蚂蚁森林")')
    first_ele.click()
except NoSuchElementException as e:
    try:
        userAccountImage = driver.find_element(AppiumBy.ID, 'com.ali.user.mobile.security.ui:id/userAccountImage')
        userAccountImage.click()
        switchLogin = driver.find_element(AppiumBy.ID, 'com.ali.user.mobile.security.ui:id/switchLoginMethodCenter')
        switchLogin.click()

        login_type = driver.find_element(AppiumBy.XPATH,
                                         "//*[@resource-id='com.alipay.mobile.antui:id/dialog_listView'][1]")
        login_type.click()

        password_ele = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("请输入登录密码")')
        # 填写登录密码
        password_ele.send_keys('这里输入你的支付宝登录密码')

        login_btn = driver.find_element(AppiumBy.ID, 'com.ali.user.mobile.security.ui:id/loginButton')
        login_btn.click()
        index_ele = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR, 'new UiSelector().text("首页")')
        index_ele.click()
        first_ele = driver.find_element(AppiumBy.ANDROID_UIAUTOMATOR,
                                        'new UiSelector().resourceId("com.alipay.android.phone.openplatform:id/app_text").text("蚂蚁森林")')
        first_ele.click()
    except NoSuchElementException as e:
        pass

# 打卡背包
driver.tap([(295, 1310)], 300)
# 七点使用加速卡
# now = time.strftime('%H', time.localtime(time.time()))
# print(now)
# if int(now) == 7:
#     # 加速卡
#     driver.tap([(725, 1095)], 300)
#     # 使用
#     driver.tap([(440, 990)], 300)
#     # 收取能量
#     driver.tap([(166, 618)], 300)
#     driver.tap([(295, 556)], 300)
#     driver.tap([(401, 550)], 300)
#     driver.tap([(499, 519)], 300)
#     driver.tap([(607, 545)], 300)
#     driver.tap([(720, 621)], 300)
# 双击卡
driver.tap([(440, 1090)], 300)
# 使用
driver.tap([(446, 1015)], 300)
# 关闭背包
driver.tap([(833, 581)], 300)
minute = time.strftime('%M', time.localtime(time.time()))
while int(minute) + 5 > int(time.strftime('%M', time.localtime(time.time()))):
    try:
        # 查询能量
        driver.tap([(790, 1306)], 300)
        # 一键收取
        driver.tap([(459, 1146)], 300)
        driver.tap([(459, 1146)], 300)

        # 查找下一个
        driver.tap([(789, 1307)], 300)

        driver.tap([(459, 1146)], 300)
        driver.tap([(459, 1146)], 300)
    except NoSuchElementException as e:
        pass
    except InvalidElementStateException as e:
        pass

driver.tap([(845, 75)], 300)
driver.close()
driver.quit()
